package com.example.administrator.xxnapp.main;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.example.administrator.xxnapp.AppPresenter;
import com.example.administrator.xxnapp.BaseActivity;
import com.example.administrator.xxnapp.IViewApp;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.VersionBean;
import com.example.administrator.xxnapp.fragment.MainFragmentManager;
import com.example.administrator.xxnapp.http.CustomStringCallback;
import com.example.administrator.xxnapp.http.HttpUrl;
import com.example.administrator.xxnapp.ui.DownLoadDialog;
import com.example.administrator.xxnapp.util.Config;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mdruby.baselibrary.utils.ABAppUtil;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.item.BaseTabItem;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

public class MainActivity extends BaseActivity implements IViewApp{
    @BindView(R.id.act_main_bottom_bar_view)
    public PageNavigationView mBottomBar;
    @BindView(R.id.act_main_framelayout)
    FrameLayout mFrameLayout;
    private NavigationController navigationController;
    private String path;
    private AppPresenter mAppPresenter;

    private MainFragmentManager mFragmentManger;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initBar() {
        super.initBar();
    }

    @Override
    public void initBeforeDatas() {
        mAppPresenter = new AppPresenter(this,new VersionBean());
    }

    @Override
    public void setListeners() {
        mFragmentManger = new MainFragmentManager(mContext,R.id.act_main_framelayout);

        navigationController = mBottomBar.custom()
                .addItem(newItem(R.mipmap.navigation_bottom_home_icon,R.mipmap.navigation_bottom_home_icon_selected,getString(R.string.title_tucao)))
                .addItem(newItem(R.mipmap.navigation_bottom_event_icon,R.mipmap.navigation_bottom_event_icon_selected,getString(R.string.title_event)))
                .addItem(newItem(R.mipmap.navigation_bottom_gallery_icon,R.mipmap.navigation_bottom_gallery_icon_selected,getString(R.string.title_picture)))
                .addItem(newItem(R.mipmap.navigation_bottom_mine_icon,R.mipmap.navigation_bottom_mine_icon_selected,getString(R.string.title_mine)))
                .build();

        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int i, int i1) {
                chooseImmersionBar(i);
                mFragmentManger.chooseFragment(i);
            }

            @Override
            public void onRepeat(int i) {
            }
        });
        mFragmentManger.chooseFragment(0);
        chooseImmersionBar(0);
    }

    private void chooseImmersionBar(int i) {


    }

    /**
     * 正常tab
     */
    private BaseTabItem newItem(int drawable, int checkedDrawable, String text){
        SpecialTab mainTab = new SpecialTab(this);
        mainTab.initialize(drawable,checkedDrawable,text);
        mainTab.setTextDefaultColor(getResources().getColor(R.color.item_title_TextColor));
        mainTab.setTextCheckedColor(getResources().getColor(R.color.base_color));
        return mainTab;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        mAppPresenter.getVersion(mContext);
    }

    private void checkPermission(String path) {
        if ((ContextCompat.checkSelfPermission(mContext,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
                ) {
            downLoad(path);
        } else {
            ActivityCompat.requestPermissions((Activity) mContext,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Config.RequestCode.DOWNLOAD_REQUEST_CODE);
        }
    }

    private void downLoad(String path){
        DownLoadDialog dialog = new DownLoadDialog(mContext);
        dialog.show();
        dialog.downLoadApp(path);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case Config.RequestCode.DOWNLOAD_REQUEST_CODE:
                if ((grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    downLoad(path);
                } else {
                    Toast.makeText(mContext, "已拒绝权限！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onGetVersionSuccess(VersionBean bean) {
        checkPermission(bean.getPath());
    }
}
